//package com.punch.course.config;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
//import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
//import io.seata.rm.datasource.DataSourceProxy;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.mybatis.spring.annotation.MapperScan;
//import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.AutoConfigureBefore;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.context.annotation.Profile;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//
//@Configuration
//@MapperScan(basePackages = {"com.punch.course.mapper"})
//@Slf4j
//@ConditionalOnClass(DruidDataSource.class)
//@AutoConfigureBefore(DataSourceAutoConfiguration.class)
//public class MybatisPlusConfiguration {
//
//    @Profile("dev")
//    @Bean
//    public PerformanceMonitorInterceptor performanceMonitorInterceptor() {
//        return new PerformanceMonitorInterceptor();
//    }
//
//    /**
//     * MP 自带分页插件
//     *
//     * @return
//     */
//    @Bean
//    public PaginationInterceptor paginationInterceptor() {
//        PaginationInterceptor page = new PaginationInterceptor();
//        page.setCountSqlParser(new JsqlParserCountOptimize(true));
//
//        return page;
//    }
//
//    /**
//     * @param sqlSessionFactory SqlSessionFactory
//     * @return SqlSessionTemplate
//     */
//    @Autowired(required = true)
//    private DataSourceProperties dataSourceProperties;
//
//    @Bean
//    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }
//
//    /**
//     * 从配置文件获取属性构造datasource，注意前缀，这里用的是druid，根据自己情况配置,
//     * 原生datasource前缀取"spring.datasource"
//     *
//     * @return
//     */
//    @Bean(name = "datasource")
//    @Primary
//    public DataSourceProxy druidDataSource() {
//
//        DruidDataSource druidDataSource = new DruidDataSource();
//        log.info("dataSourceProperties.getUrl():{}", dataSourceProperties);
//        druidDataSource.setUrl(dataSourceProperties.getUrl());
//        druidDataSource.setUsername(dataSourceProperties.getUsername());
//        druidDataSource.setPassword(dataSourceProperties.getPassword());
//        druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
//        druidDataSource.setInitialSize(1);
//        druidDataSource.setMaxActive(120);
//        druidDataSource.setMaxWait(60000);
//        druidDataSource.setMinIdle(1);
//        druidDataSource.setValidationQuery("Select 1 from DUAL");
//        druidDataSource.setTestOnBorrow(false);
//        druidDataSource.setTestOnReturn(false);
//        druidDataSource.setTestWhileIdle(true);
//        druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
//        druidDataSource.setMinEvictableIdleTimeMillis(25200000);
//        druidDataSource.setRemoveAbandoned(true);
//        druidDataSource.setRemoveAbandonedTimeout(1800);
//        druidDataSource.setLogAbandoned(true);
//        log.info("装载dataSource........");
//        return new DataSourceProxy(druidDataSource);
//    }
//
//
//    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
////         DataSource dataSourceProxy = new DataSourceProxy(druidDataSource);
//
//        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
//        bean.setDataSource(dataSourceProxy);
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        // bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
//        bean.setMapperLocations(resolver.getResources("classpath*:/mapper/*.xml"));
//        SqlSessionFactory factory = null;
//        try {
//            factory = bean.getObject();
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return factory;
//    }
//}
